/*
 * PWM_Config.c
 *
 *  Created on: Nov 5, 2013
 *      Author: BlackVolt
 */


#include "derivative.h"
#include "Mytypes.h"
#include "PWM_Config.h"


/**************************************************************
 *  Name                 : vdfn_PwmInit
 *  Description          : Initialize needed system drivers (system elements) before start application
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/

void vdfn_PwmInit(){
	
	//Variable Init
	K1 = 205;
	K2 = -5;
	Reference = 100;
	
	// clock source select PLL or FLL
	SIM_SOPT2 |= SIM_SOPT2_PLLFLLSEL_MASK; //Select PLL clock
	SIM_SOPT2 |= SIM_SOPT2_TPMSRC(1);	//MC6PLLCLK/2 = 48Mhz/2 = 24Mhz
	
	// System Clock Gating (Enable)
	SIM_SCGC6 |= SIM_SCGC6_TPM0_MASK;
	
	//********************

	
	//TPM0_SC |= TPM_SC_PS(3); //~TOIE_MASK; //Disable Interrupt

	TPM0_SC |= TPM_SC_PS(3); 		//CLOCK 3MHZ


	//Select the Counter max value 100% or 0xFFFF
	TPM0_MOD = PWM_20ms_PERIOD;
	
	//Configures the PWM (No interrupt)
	TPM0_C0SC |=  TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK;	//For Center Aligned PWM
	TPM0_C1SC |=  TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK;	//Reverse
	TPM0_C2SC |=  TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK;
	TPM0_C3SC |=  TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK;	//Reverse
	
	
	
	//Configure the initial value for the counter in this case 50%
	TPM0_C0V = PWM_HALF_COUNT;
	TPM0_C1V = 0;
	TPM0_C2V = PWM_HALF_COUNT;
	TPM0_C3V = 0;
	
	
	//Enable the Counter
	TPM0_SC |= TPM_SC_CMOD(1);
	
	


}

/**************************************************************
 *  Name                 : vdfn_Pwm_MASK
 *  Description          : Function called in Scheduler
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/

void vdfn_Pwm_MASK (){
	
	u16fn_PI_Controler(Frecuency_Real,Reference); 
	
}


/**************************************************************
 *  Name                 : u16fn_PI_Controler
 *  Description          : Change the Value of a Variable
 *  Parameters           : Frec_Real & Reference
 *  Return               : none
 *  Critical/explanation : This calculate the new value of PWM
 **************************************************************/

void u16fn_PI_Controler(uint16 Frec_Real, uint16 Reference){
	//Calculate the Error

	Error = Reference -Frec_Real;
	
	//Use the Controler
	U = U +(K1 * Error) + (K2 * Error1);
	
	//Lock avoid overflow trouble
	if(U > 150){ U = 150;}
	if(U < 20){U = 20;}
	
	Error1 = Error;
	u16fn_Pwm_BW(U);
	
}



/**************************************************************
 *  Name                 : u16fn_Pwm_BW
 *  Description          : Change the PWM Band With 
 *  Parameters           : uint16 it means the Value of PWM
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/
void u16fn_Pwm_BW (uint16 u16_Count){
	
	/*
	 * PI CODE  
	 */

	
	TPM0_C0V = u16_Count * 400; //This must be uncommet if is not a test
	
	TPM0_C2V = u16_Count * 400; 
	
	if(ON_OFF_FLAG){
		TPM1_C1V = u16_Count * 400;
	}
	else{
		TPM1_C1V = 0;}
	
	
	/*
	 * TEST CODE 
	 */
	//TPM0_C0V = ~u16_Count;
	//TPM0_C2V = ~u16_Count;
	
	
}



/**************************************************************
 *  Name                 : vdfn_Pwm_MASK_Test
 *  Description          : Function test called in Scheduler 
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/

void vdfn_Pwm_MASK_Test (){
	u16fn_Pwm_TEST();			//This is a test only Function
	u16fn_Pwm_BW(uin16_PWM_Lenght);		//This function modify the BW of the PWM
}



/**************************************************************
 *  Name                 : u16fn_Pwm_TEST
 *  Description          : Change the Value of a Variable
 *  Parameters           : none
 *  Return               : none
 *  Critical/explanation : no
 **************************************************************/

void u16fn_Pwm_TEST(){
	
	uin16_PWM_Lenght = PWM_80_PERCENT;
	
	if(uin16_PWM_Lenght < 0x0100){
		
		uin16_PWM_Lenght = 0xFFFE;
	}
	
}





